package com.ibeetl.admin.aod.service.excel.parser;

import com.ibeetl.admin.aod.entity.AodIndustryData;
import com.ibeetl.admin.aod.entity.AodIndustryKeyword;
import com.ibeetl.admin.aod.util.DateUtils;
import com.ibeetl.admin.aod.util.ToolUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class FilterFileParser extends AbstractFileParser {

    @Override
    public AodIndustryData parseFilter(String line, List<AodIndustryKeyword> aodIndustryKeywordList, String account) {
        AodIndustryData aodIndustryData = null;
        String[] lines = line.split(",");
        if (null != lines && lines.length >= 8) {
            aodIndustryData = new AodIndustryData();
            aodIndustryData.setAccount(ToolUtil.nullToString(lines[0]));
            aodIndustryData.setPhoneNumber(ToolUtil.nullToString(lines[1]));
            aodIndustryData.setSendTime(DateUtils.parseToDateTime(lines[2]));
            aodIndustryData.setSendContent(ToolUtil.nullToString(lines[3]));
            aodIndustryData.setReplyContent(ToolUtil.nullToString(lines[6]));
            aodIndustryData.setLabel(ToolUtil.nullToString(lines[7]));
            aodIndustryData.setLevel(ToolUtil.nullToInt(lines[8]));
            aodIndustryData.setAddTime(new Date());
            if (lines.length >= 10) {
                aodIndustryData.setNumberAttribution(ToolUtil.nullToLong(lines[9]));
            }

            if (StringUtils.isNotEmpty(account)) {
                aodIndustryData.setIndustryId(matchIndustryCondition(lines[0], lines[3], aodIndustryKeywordList, account));
            } else {
                aodIndustryData.setIndustryId(matchIndustry(lines[3], aodIndustryKeywordList));
            }


//            if (lines[7].contains(aodIndustryKeyword.getKeyword())) {
//                // 关键词匹配上了 设置对应的行业类别ID
//                aodIndustryData.setIndustryId(aodIndustryKeyword.getIndustryId());
//            } else {
//                // 默认为 无行业类别
//                aodIndustryData.setIndustryId(AodIndustryData.NON_INDUSTRY_ID);
//            }
        }
        return aodIndustryData;
    }

    private Long matchIndustryCondition(String excelAccount, String sendContent, List<AodIndustryKeyword> aodIndustryKeywordList, String account) {

        if (!excelAccount.equals(account)) {
            return AodIndustryData.NON_INDUSTRY_ID;
        }

        if (CollectionUtils.isNotEmpty(aodIndustryKeywordList)) {
            for (AodIndustryKeyword aodIndustryKeyword : aodIndustryKeywordList) {
                if (sendContent.contains(aodIndustryKeyword.getKeyword())) {
                    return aodIndustryKeyword.getIndustryId();
                }
            }
        }
        return AodIndustryData.NON_INDUSTRY_ID;

    }

    private Long matchIndustry(String sendContent, List<AodIndustryKeyword> aodIndustryKeywordList) {

        if (CollectionUtils.isNotEmpty(aodIndustryKeywordList)) {
            for (AodIndustryKeyword aodIndustryKeyword : aodIndustryKeywordList) {
                if (sendContent.contains(aodIndustryKeyword.getKeyword())) {
                    return aodIndustryKeyword.getIndustryId();
                }
            }
        }
        return AodIndustryData.NON_INDUSTRY_ID;

    }

}
